#
# Tool Configuration
#

# Instruction Set Selection - * Valid Values are:  ARM32 or THUMB
INSTRUCTION_SET=ARM32

# Compiler Selection ADS or SDT
COMPILER_TYPE = ADS


# Tools
CC = armcc
NATIVECC = armcc
ASM = armasm
LD = armlink
PRD = RDA_HID # for HID

COMPILER_FLAGS = -littleend -g -zo $(THUMB_COMPILER_FLAGS) -DADS_TOOLCHAIN_SUPPORTED=1 -DR5871_PRODUCT=${PRD} # for ADS 1.x
TOOL_SPECIFIC_ASM_FLAGS = -PD "ADS_TOOLCHAIN_SUPPORTED SETL {TRUE}" -PD "RDA_BLUETOOTH SETL {TRUE}"
SDT_ZEROPAD = 
ADS_OUTPUT_FLAG = -output
SDT_SYMBOLS = 

MKLIB = armar
MKLIB_FLAGS = -create
FLASH_SCATTER_FILE = $(HAL_PLATFORM_DIR)/sys/scatter_map/flash_map_ads.txt
DEBUG_SCATTER_FILE = $(HAL_PLATFORM_DIR)/sys/scatter_map/debug_map_ads.txt

OPTIMIZATIONS = -O2 -Ospace -fpu NONE
TOP_OF_MEMORY=0x80008000
SCATTER_FILE = flash_map_ads.txt

CONFIG_FLAGS_FIXED = \
    -DRDA_BLUETOOTH \
    -DJAL_BASE_ADDR=0x70000000 \
    -DHW_DATA32 \
    -D__USE_INLINES__ \
    -DNO_STOP_ON_ASSERT_FAIL \
    -D__BYTE_ORDER=__LITTLE_ENDIAN \
    -DPRH_BS_CFG_SELECT_LEVEL=5 \
    -DTOP_OF_MEMORY=$(TOP_OF_MEMORY) \
    -DBUILD_TYPE=FLASH_BUILD \
    -DBUILD_EMULATION=0 \
    -DBUILD_MEMORY_MAP=2 \
    -DBUILD_PLATFORM=armcc \
    -DRDA_PRODUCT=RDA_HID_HEADSET \
    -DCOMBINED_HOST=1 \
    -DPRH_BS_CFG_TABASCO_VERSION=PRH_BS_HW_TABASCO_VERSION_T1_0_1 \
    $(RF_FLAGS)
    

ASM_FLAGS = -littleend -g -PD "BUILD_FOR_ROM SETL {TRUE}" \
     -PD "TOP_OF_MEMORY SETA $(TOP_OF_MEMORY)" \
     -PD "SYSboot_TARGET_IS_CHIMERA SETL {TRUE}" \
     -PD "FAST_SRAM_SUPPORTED SETL {TRUE}" \
     $(TOOL_SPECIFIC_ASM_FLAGS)
CONFIG_FLAGS = $(CONFIG_FLAGS_FIXED) \
    $(CONFIG_FLAGS_VARIABLE) \
    $(CONFIG_FLAGS_UNDER_TEST) \
    $(CONFIG_FLAGS_OVERRIDE) 

# LD_FLAGS = -entry SYS_Vector_Init_Block -remove -info Totals,Sizes -scatter $(SCATTER_FILE) -symdefs bt_hid.sym  -map $(SDT_SYMBOLS) > mapfile.txt 2>&1
LD_FLAGS = -entry SYS_Vector_Init_Block -remove -info Totals,Sizes -scatter $(SCATTER_FILE)  -map $(SDT_SYMBOLS) > mapfile.txt 2>&1


HAL_DIR = src/hal

INCLUDES = -Iinclude/core -Iinclude/lmp -Iinclude/host -Iinclude/l2cap -Iinclude/hid

PATCH_OBJECTS = \
	src/patch.o

HAL_DEPENDENCIES = \
	$(HAL_DIR)/sys_fiq_irq.o		\
	$(HAL_DIR)/sys_vctr.o		\
	$(HAL_DIR)/sys_main.o		\
	$(HAL_DIR)/sys_boot.o		\

LIBS = \
	lib/prth_chimera_be_hal.lib	\
	lib/arm7tdmi_be_core.lib	\
	lib/arm7tdmi_be_host.lib	\



test_bt.axf: $(HAL_DEPENDENCIES) $(PATCH_OBJECTS)
	$(LD) -o $@ $^ $(LD_FLAGS) $(LIBS)
	fromelf -c test_bt.axf > test_bt.asm
	fromelf -s test_bt.axf > patch.sym
clean:
	rm -rf $(HAL_DEPENDENCIES) $(PATCH_OBJECTS) test_bt.axf test_bt.asm patch.sym mapfile.txt 

$(HAL_PLATFORM_DIR)/sys/sys_main.o: $(HAL_PLATFORM_DIR)/sys/sys_main.c
	$(NATIVECC) -c $(COMPILER_FLAGS) $(CONFIG_FLAGS) $(OPTIMIZATIONS) $(INCLUDES) $(PLATFORM_INCLUDES) $< -o $@ 2>>error.log

$(HAL_PLATFORM_DIR)/sys/sys_irq.o: $(HAL_PLATFORM_DIR)/sys/sys_irq.c
	$(NATIVECC) -c $(COMPILER_FLAGS) $(CONFIG_FLAGS) $(OPTIMIZATIONS) $(INCLUDES) $(PLATFORM_INCLUDES) $< -o $@ 2>>error.log

###########################################################################
#
# PATTERN RULES
#


#
# Objects from C
#
%.o: %.c 
	$(CC) -c $(COMPILER_FLAGS) $(CONFIG_FLAGS) $(OPTIMIZATIONS) $(INCLUDES) $(PLATFORM_INCLUDES) $< -o $@ 2>>error.log

#
# Objects from Asm
%.o: %.s 
	$(ASM) $(ASM_FLAGS) $< -o $@ 2>>error.log

#
# Objects from Asm
%.o: %.asm 
	$(ASM) $(ASM_FLAGS) $< -o $@ 2>>error.log

 # Objects from GNU asm
 %.o: %.S 
	$(CC) -c $(COMPILER_FLAGS) $(CONFIG_FLAGS) $(OPTIMZATIONS) $(INCLUDES) $(PLATFORM_INCLUDES) $< -o $@ 2>>error.log

#
# Asm from C
#
%.s: %.c 
	$(CC) -s $(COMPILER_FLAGS) $(CONFIG_FLAGS) $(OPTIMIZATIONS) $(INCLUDES) $(PLATFORM_INCLUDES) $< -o $@

#
# Postprocessed C from Preprocessed C
#
%.e: %.c 
	$(CC) -E $(COMPILER_FLAGS) $(CONFIG_FLAGS) $(OPTIMIZATIONS) $(INCLUDES) $(PLATFORM_INCLUDES) $< -o $@
